<template>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto">
        <el-form-item label="请输入账号" prop="username">
            <el-input v-model="ruleForm.username" />
        </el-form-item>
        <el-form-item label="请输入密码" prop="password">
            <el-input v-model="ruleForm.password" />
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
                注册
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { reactive, ref } from 'vue'
const ruleFormRef = ref()
const ruleForm = reactive({
    username: '',
    password: '',
})
const rules = reactive({
    username: [
        { required: true, message: '请填写账号', trigger: 'blur' },
        { min: 3, max: 10, message: '3 to 10', trigger: 'blur' },
    ],
    password: [
        { required: true, message: '请填写密码', trigger: 'blur' },
        { min: 3, max: 10, message: '3 to 10', trigger: 'blur' },
    ],
})
import { ElMessage } from 'element-plus'
import http from '@/api/http'
const submitForm = async (formEl) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            http.post('/register',ruleForm).then(res => {
                console.log('注册', res);

                if (res.data.code == 10000) {
                    ElMessage({
                        message: '注册成功',
                        type: 'success',
                    })
                }
                else {
                    ElMessage.error('注册失败')
                }
            })
        } else {
            console.log('error submit!', fields)
        }
    })
}
</script>

<style lang="scss" scoped></style>